package com.splant.android.smart.SystemDefinedLevel.AutoUpdateSoft;

import android.content.Context;
import android.content.DialogInterface;
import android.widget.Toast;

import com.splant.android.smart.SystemDefinedLevel.GenericControls.Dialog.CustomDialog;
import com.splant.android.smart.SystemDefinedLevel.Notifications.UpdateVersionNotification;

/*版本更新*/
public class SoftVersionManager {

    private UpdateVersionNotification mUpdateNotify;
    private UpdateManager mUpdateManager;
    private Context mContext;
    public SoftVersionManager(Context context) {
        mContext=context;
        mUpdateManager = new UpdateManager(context, updateCallback);
        mUpdateNotify = new UpdateVersionNotification(context);
    }

    private UpdateManager.UpdateCallback updateCallback=new UpdateManager.UpdateCallback()
    {

        @Override
        public void checkUpdateCompleted(Boolean hasUpdate, String updateTitle, String updateMessage)
        {
            if (hasUpdate)
            {
                showUpdateDialog(updateTitle,updateMessage);
            }
            else
            {
               // if (flag)
                  //  Toast.makeText(mContext,"best new version!",Toast.LENGTH_SHORT).show();
            }

        }
        @Override
        public void downloadProgressChanged(int progress) {

            mUpdateNotify.showNotifyProgress(progress);
        }
        @Override
        public void downloadCanceled() {

            Toast.makeText(mContext,"DownLoad Canceled!",Toast.LENGTH_SHORT).show();
        }
        @Override
        public void downloadCompleted(Boolean sucess, CharSequence errorMsg) {

            if (sucess)
            {
                mUpdateNotify.cancelNotifyProgress();
                mUpdateManager.popInstall();
            }
            else
                Toast.makeText(mContext,errorMsg.toString(),Toast.LENGTH_SHORT).show();
        }
    };

    private void showUpdateDialog(final String updateTitle, String updateMessage)
    {
        CustomDialog.Builder builder = new CustomDialog.Builder(mContext);
        builder.setMessage(updateMessage);
        builder.setTitle(updateTitle);
        builder.setPositiveButton(mContext.getString(android.R.string.ok), new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which)
            {
                mUpdateNotify.showNotifyProgress(0);
                mUpdateManager.downloadPackage();
                dialog.dismiss();
            }
        });
        builder.setNegativeButton(mContext.getString(android.R.string.cancel),
                new android.content.DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {

                        dialog.dismiss();
                    }
                });
        builder.create().show();
    }

    public void downLoadFile(String apkUrl,String title,String msg)
    {
        mUpdateManager.setApkFileUrl(apkUrl);
        updateCallback.checkUpdateCompleted(true,title,msg);
    }

    private boolean flag;
    public void checkVersion(boolean isFlag)
    {
        flag=isFlag;
        mUpdateManager.checkVersion();
    }

}
